package edu.gmu.swe632.fruit.activity;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import edu.gmu.swe632.fruit.R;
import edu.gmu.swe632.fruit.service.GestureRecognizerService;
import edu.gmu.swe632.fruit.util.CustomAdapter;
import edu.gmu.swe632.fruit.util.RowData;
import event.GestureEvent;

public class SosActivity extends GestureListActivity
{
	private static final String TAG = "sos";
	private static final String[] title = new String[] { "Confirm" };
	private static final String[] detail = new String[] { "" };
	private static final Integer[] imgid = { R.drawable.check };
	private static final Class<?> nextIntentClasses[] = { FruitMainActivity.class };

	public static final String SENDING = "Sending SOS!";

	private LayoutInflater mInflater;
	private Vector<RowData> data;
	private RowData rd;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.sos);

		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		data = new Vector<RowData>();

		for (int i = 0; i < title.length; i++)
		{
			try
			{
				rd = new RowData(i, title[i], detail[i]);
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			data.add(rd);
		}

		CustomAdapter adapter = new CustomAdapter(this, mInflater, R.layout.list_item, R.id.title,
				data, imgid);
		setListAdapter(adapter);
		getListView().setTextFilterEnabled(true);
	}

	@Override
	public void onListItemClick(ListView parent, View v, int position, long id)
	{
		startNextActivity(position);
	}

	@Override
	public void gestureReceived(GestureEvent event)
	{
		if (event.getProbability() > 0.9)
		{
			String gesture = GestureRecognizerService.GestureIdMapping.get(event.getId());

			Log.d(TAG, "Gesture received " + gesture);

			if (gesture.compareTo(GestureRecognizerService.CHECK_GESTURE) == 0)
				startNextActivity(0);
		}
	}

	private void startNextActivity(int index)
	{
		if (0 <= index && index < nextIntentClasses.length)
		{
			Toast.makeText(this, SENDING, Toast.LENGTH_LONG).show();

			startActivity(new Intent(this, nextIntentClasses[index]));
		}
	}
}
